#include "bullet.h"

const qreal Bullet::m_speed = 10.0;

Bullet::Bullet(qreal x, qreal y, qreal dirX, qreal dirY)
    : m_x(x), m_y(y), m_dirX(dirX), m_dirY(dirY)
{
    m_pixmap = QPixmap("img/bullet.png");
}

Bullet::~Bullet()
{

}

void Bullet::draw(QPainter *painter)
{
    painter->drawPixmap(m_x - m_pixmap.width() / 2.0, m_y - m_pixmap.height() / 2.0, m_pixmap);
}

void Bullet::move()
{
    m_x += m_dirX * m_speed;
    m_y += m_dirY * m_speed;
}

bool Bullet::isOutOfField(qint32 width, qint32 height)
{
    return m_x < 0.0 || m_y < 0.0 || m_x > width || m_y > height;
}

bool Bullet::intersects(Asteroid *asteroid)
{
    qreal ax = asteroid->getX();
    qreal ay = asteroid->getY();
    qreal rad = asteroid->getRadius();
    return sqrt((ax - m_x) * (ax - m_x) + (ay - m_y) * (ay - m_y)) < rad + m_pixmap.width() / 2.0;
}
